//
// Created by Senhai Xu on 2025/3/17.
//
struct ListNode
{
  int val;
  struct ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
};
//BM14 链表的奇偶重排
class Solution {
public:
  /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
   */
  ListNode* oddEvenList(ListNode* head) {
    if (head == nullptr || head->next == nullptr)  return head;
    // 定义奇偶指针
    ListNode* odd = head;
    ListNode* even = head->next;
    ListNode* evenhead = even;
    while (even != nullptr && even->next != nullptr) {
      odd->next = even->next;
      odd = odd->next;
      even->next = odd->next;
      even = even->next;
    }
    odd->next = evenhead;
    return head;
    
  }
};